﻿using System;
using System.Data;
using System.Runtime.Serialization;

namespace OpenComposite.HumanActivity.Contracts
{
	[DataContract(Name = "Note", Namespace = "http://www.theopencomposite.com/humanactivities")]
	public class NoteItem : ItemBase
	{
		#region Initialization
		public NoteItem(Guid workflowInstanceId, int? activityId, int userId)
		{
			this.WorkflowInstanceId = workflowInstanceId;
			this.ActivityId = activityId;
			this.CreatedById = userId;
			this.CreatedDateTime = DateTime.Now;
		}
		public NoteItem(DataRow rowNote)
		{
			try {
				this.NoteId = (int)rowNote["note_id"];
				this.WorkflowInstanceId = (Guid)rowNote["workflow_instance_id"];
				this.ActivityId = getNullableInt(rowNote, "activity_id");

				this.Subject = rowNote["note_subject"] as string;
				this.Text = rowNote["note_txt"] as string;

				this.CreatedById = (int)rowNote["created_by"];
				this.CreatedDateTime = getNullableDateTime(rowNote, "created_dt");

				this.LastModifiedById = getNullableInt(rowNote, "lastmodified_by");
				this.LastModifiedDateTime = getNullableDateTime(rowNote, "lastmodified_dt");
			} catch ( Exception ex ) {
				throw new InvalidOperationException("Invalid notes datarow.", ex);
			}
		}
		#endregion

		#region Data Members
		[DataMember]
		public int? NoteId { get; private set; }
		[DataMember]
		public Guid WorkflowInstanceId { get; private set; }
		[DataMember]
		public int? ActivityId { get; private set; }

		[DataMember]
		public string Subject { get; set; }
		[DataMember]
		public string Text { get; set; }

		[DataMember]
		public int CreatedById { get; private set; }
		[DataMember]
		public DateTime? CreatedDateTime { get; private set; }
		[DataMember]
		public int? LastModifiedById { get; set; }
		[DataMember]
		public DateTime? LastModifiedDateTime { get; private set; }
		#endregion
	}
}
